//
//  GroupViewController.m
//  TrueLove
//
//  Created by 刘康 on 15/1/29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "GroupViewController.h"
#import "MyGroupCell.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "BrideDetail.h"
#import "MyGroupDetailCell.h"
#import "GroupDetailViewController.h"

@interface GroupViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *array;



@end

@implementation GroupViewController

- (void)dealloc
{
    [_tableView release];
    [_groupBride release];
    [_array release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"小组";
    [self createTableView];
    [self netWorkingRequestForGroup];
    
    
}

- (void)netWorkingRequestForGroup
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    NSString *str = [NSString stringWithFormat:@"http://www.hunliji.com/v1/api/app/community/threads?group_id=%@&per_page=20&refined=0&page=1",[self.groupBride.myId stringValue]];
//    NSLog(@"%@",str);
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        NSArray *arr = [NSArray arrayWithArray:[dic objectForKey:@"threads"]];
        self.array = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            BrideDetail *brideDetail = [[BrideDetail alloc] init];
            [brideDetail setValuesForKeysWithDictionary:dic];
            [self.array addObject:brideDetail];
            [brideDetail release];
            [_tableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@",error);
    }];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 110;
    } else {
        return 70;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *str = @"reues0";
        MyGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[[MyGroupCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.MyImageView setImageWithURL:[NSURL URLWithString:self.groupBride.image]];
        cell.titleLabel.text = self.groupBride.title;
        cell.descriptionLabel.text = self.groupBride.MyDescription;
        cell.todayLabel.text = [NSString stringWithFormat:@"今日%@",[self.groupBride.added_threads_count stringValue]];
        cell.browseLabel.text = [[self.groupBride.click_count stringValue] stringByAppendingString:@"浏览"];
        return cell;
    } else {
        
        static NSString *str = @"reuse";
        MyGroupDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[[MyGroupDetailCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
        }
        //取消选中的状态效果
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        BrideDetail *brideDetail = [self.array objectAtIndex:indexPath.row - 1];
        
        cell.titleLabel.text = brideDetail.title;
        
        cell.userNameLabel.text = [brideDetail.author objectForKey:@"nick"];
        NSString *string = [brideDetail.author objectForKey:@"img"];
            [cell.userImageView setImageWithURL:[NSURL URLWithString:string]];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //让第一行点击没反应
    if (indexPath.row >= 1) {
        GroupDetailViewController *groupDetailVC = [[GroupDetailViewController alloc] init];
        
        groupDetailVC.userDetail = [self.array objectAtIndex:indexPath.row - 1];
        
        [self.navigationController pushViewController:groupDetailVC animated:YES];
        [groupDetailVC release];
    }
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
